package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import service.OrderService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * ClassName: OrderServiceImpl
 * Description:
 * date: 2019/11/26 14:25
 * Author Airson
 * Since JDK 1.8
 */
@Service
@Transactional
@Slf4j
public class OrderServiceImpl implements OrderService {

    //预约设置注入
    @Autowired
    OrderSettingDao orderSettingDao;

    //会员信息注入
    @Autowired
    MemberDao memberDao;
    //预约信息注入

    @Autowired
    OrderDao orderDao;

    /**
     * 预约添加
     *
     * @param map
     * @return
     */
    @Override
    public Result add(Map<String, String> map) {
        try {
        log.debug("map:{}",map);
        //检查预约日期是否设置,在t_orderSetting表格中查询日期
        //获取预约电话
        String telephone = map.get("telephone");
        //获取预约套餐的id
        Integer setmealId = Integer.parseInt(map.get("setmealId"));
        //获取预约日期
        String orderDatestr = map.get("orderDate");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");         //日期格式转换
        Date orderDate = null;

            orderDate = simpleDateFormat.parse(orderDatestr);

        //获取预约姓名
        String name = map.get("name");
        //获取预约性别
        String sex = map.get("sex");
        //获取预约的身份证号
        String idCard = map.get("idCard");

        OrderSetting orderSetting = orderSettingDao.findOrderSettingByDate(orderDate);

        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //检查预约日期是否约满,在t_oderSetting表中查询number>reservation
        if (orderSetting.getNumber() <= orderSetting.getReservations()) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //判断用户是不是会员,不是添加信息到会员,查询t_member表中是否有该用户的telephone,
        Member member = memberDao.findMemberByTelephoneNumber(telephone);
        if (member == null) {
            //新建用户对象
            member = new Member();
            //设置对象
            SimpleDateFormat simple = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String filenumber = simple.format(new Date()) + ValidateCodeUtils.generateValidateCode(4);
            member.setFileNumber(filenumber);   //用户编号
            member.setRegTime(new Date());      //注册时间
            member.setIdCard(idCard);    //身份证
            member.setName(name);        //姓名
            member.setSex(sex);          //性别
            member.setPhoneNumber(telephone);       //电话

            //注册对象
            memberDao.add(member);
        } else {
            //如果是会员,更新会员信息
            //3.2.1 更新会员的信息
            member.setName(name);
            member.setSex(sex);
            member.setIdCard(idCard);
            //更新会员信息
            memberDao.edit(member);

            //检查该用户的预约记录(防止重复预约),在t_order表中,根据用户member_id和日期orderDate查询和预约类型orderType
            Order memberOrder = new Order();
            memberOrder.setMemberId(member.getId());
            memberOrder.setOrderDate(orderDate);
            memberOrder.setSetmealId(setmealId);

            long counnt = orderDao.findbymemberId(memberOrder);
            if (counnt > 0) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setOrderType(map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(setmealId);

        //添加用户预约信息到预约表格中
        orderDao.add(order);

        //5. 更新预约设置信息： 已预约人数加一

        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.edit(orderSetting);
        log.debug("返回值打印--------------------:{}",order);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    @Override
    public void findById(Integer id) {

    }
}
